package com.jiyun.information.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.jiyun.information.mapper.InformationMapper;
import com.jiyun.information.service.InformationService;
import com.jiyun.pojo.chat.ChatMessage;
import com.jiyun.pojo.driver.DriverAttribute;
import com.jiyun.pojo.information.vo.ComplaintVo;
import com.jiyun.pojo.information.vo.MessageVo;
import com.jiyun.pojo.order.OrderComplaint;
import com.jiyun.pojo.order.OrderInformation;
import com.jiyun.pojo.order.OrderTime;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.stream.Collectors;

@Slf4j
@Service
public class InformationServiceImpl implements InformationService {
    @Autowired
    private InformationMapper informationMapper;
    @Autowired
    private ThreadPoolTaskExecutor goodsThreadPool;

    @Override
    public List<ComplaintVo> complaintList(String userId) {
        //创建返回vo对象
        ArrayList<ComplaintVo> list = new ArrayList<>();
        //查找订单基础信息
        List<OrderInformation> orderInformations = informationMapper.getOrderInformationList(userId);
        for (OrderInformation orderInformation : orderInformations) {
            ComplaintVo complaintVo = new ComplaintVo();
            //查找投诉表的信息
            CompletableFuture<OrderComplaint> orderComplaint = CompletableFuture.supplyAsync(() ->
                    informationMapper.getOrderComplaint(orderInformation.getOrderId()), this.goodsThreadPool
            );
            //根据投诉的订单查找订单时间
            CompletableFuture<OrderTime> orderTime = CompletableFuture.supplyAsync(() ->
                    informationMapper.getOrderTime(orderInformation.getOrderId()), this.goodsThreadPool
            );
            //等待上面执行结束
            CompletableFuture.allOf(orderComplaint, orderTime).join();
            //赋值
            complaintVo.setOrderInformation(orderInformation);
            complaintVo.setOrderComplaint(orderComplaint.join());
            complaintVo.setOrderTime(orderTime.join());
            if (ObjectUtil.isNotEmpty(complaintVo.getOrderComplaint())) {
                list.add(complaintVo);
            }
        }

        return list;
    }

    @Override
    public OrderComplaint getComplaint(String orderId) {
        return informationMapper.getComplaint(orderId);
    }

    @Override
    public List<HashMap<Integer, Integer>> getOrderStatusCount(String userId) {
        return informationMapper.getOrderStatusCount(userId);
    }

    @Override
    public List<MessageVo> messageList(String userId) {
        List<MessageVo> list = new ArrayList<>();
        //根据用户ID来查找订单信息
        List<OrderInformation> orderInformations = informationMapper.getOrderInformationList(userId);
        for (OrderInformation information : orderInformations) {
            MessageVo vo = new MessageVo();
            //查找订单时间表的信息
            CompletableFuture<OrderTime> orderTime = CompletableFuture.supplyAsync(() ->
                    informationMapper.getOrderTime(information.getOrderId()), this.goodsThreadPool
            );
            if (ObjectUtil.isNotEmpty(information.getDriverId())) {
                //根据订单查找司机信息
                CompletableFuture<DriverAttribute> driverAttribute = CompletableFuture.supplyAsync(() ->
                        informationMapper.getDriverAttribute(information.getDriverId()), this.goodsThreadPool
                );

                //查找消息列表
                CompletableFuture<ChatMessage> chatMessage = CompletableFuture.supplyAsync(() ->
                        informationMapper.getChatMessage(information.getUserId(), information.getDriverId()), this.goodsThreadPool
                );

                CompletableFuture.allOf(driverAttribute, chatMessage).join();
                vo.setDriverAttribute(driverAttribute.join());
                vo.setChatMessage(chatMessage.join());
            }

            //等待上面执行结束
            CompletableFuture.allOf(orderTime).join();
            //赋值
            vo.setOrderInformation(information);
            vo.setOrderTime(orderTime.join());

            list.add(vo);
        }
        //根据订单中的司机去重
        List<MessageVo> collect = list.stream().collect(Collectors.toMap(MessageVo::getDriverAttribute,
                        m -> m, (existing, replacement) -> existing))
                .values().stream().collect(Collectors.toList());
        return collect;
    }
}


